<template>
  <el-drawer
    title="选择文案"
    :visible.sync="creativeTextSelection"
    :modal-append-to-body="false"
    :close-on-click-modal="false"
    :wrapperClosable="false"
    :modal="false"
    @close="handleTextSelectionClose"
    size="60%"
  >
    <el-divider></el-divider>
    <div>
      <!--                       @input="searchCreativeText"-->
      <el-row style="margin-left: 10px;margin-right: 10px">
        <el-col :span="18">
          <el-input v-model="textSearchData" style="width:100%"
                    @input="searchCreativeText"
                    placeholder="请输入要搜索的内容"></el-input>
        </el-col>
        <el-col :span="6">
          <div style="color:#72767b; margin-left: 50px;margin-top: 8px;font-size: 14px">
            已选中 <span style="font-size: 20px; color:#00a680 !important;">{{ textSelectData.length }}</span> 个文案
          </div>
        </el-col>
      </el-row>
      <div class="drawer__content">
        <el-table
          ref="acoTexts"
          row-key="id"
          :data="filterTextData"
          @selection-change="handleTextSelectionChange"
        >
          <el-table-column type="selection" width="55" :reserve-selection="true"/>
          <el-table-column label="广告文案" prop="text" width="200"/>
          <el-table-column label="语言" prop="language"/>
          <el-table-column label="投放媒体" prop="platform"/>
          <el-table-column label="解释" prop="description" width="200"/>
        </el-table>
        <div class="bottom-page clearfix">
          <el-pagination
            @current-change="handleTextCurrentChange"
            :current-page="textPage.current"
            :page-sizes="[textPage.pageSize]"
            :page-size="textPage.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="searchTextData.length">
          </el-pagination>
        </div>
        <div class="drawer__footer">
          <el-button type="primary" @click="confirmTextSelection" style="margin:10px">确定</el-button>
        </div>
      </div>
    </div>
  </el-drawer>
</template>

<script>
  import {getTextList} from "@/api/campaign-management/campaign-management";

  export default {
    name: "CreativeTextDrawer",
    props:{
      visibletext: {
        type:Boolean,
        default: false
      },
      docs:{
        type:Array,
      },
      offerId:String
    },
    data(){
      return {
        textData: [],
        textSearchData: '',
        searchTextData: [],
        textSelectData: [],
        creativeTexts: [],
        textPage: {
          pageSize: 8,
          current: 1,
          total: 0
        },
      }
    },
    computed: {
      creativeTextSelection: {
        get() {
          return this.visibletext
        },
        set(val) {
          this.$emit('update:visibletext', val || false)
        }
      },
      filterTextData() {
        return this.searchTextData.slice((this.textPage.current - 1) * this.textPage.pageSize,
          this.textPage.current * this.textPage.pageSize)
      }
    },
    created() {
      this.getTextData()
    },
    methods:{
      confirmTextSelection(){
        if (this.textSelectData.length === 0) return
        if (this.textSelectData.length + this.docs.length > 5) return this.$message.error('程序化创意文案最多只能存在5条')
        const arr = this.docs.concat(this.textSelectData.map(item => {
          return item
        }))
        let newArr = arr.reduce((pre, cur) => {
          if (!pre.some(item => item === cur)) {
            return pre.concat(cur)
          } else {
            return pre
          }
        }, [])
        this.$emit("changeDocs", newArr)
        this.creativeTexts = []
        this.textSelectData = []
        this.creativeTextSelection = false
      },
      handleTextSelectionClose(){
        this.creativeTextSelection = false
        this.creativeTexts = []
        this.textSelectData = []
      },
      searchCreativeText(val) {
        if (this.textSearchData.length === 0)
          this.searchTextData = this.textData
        else {
          this.searchTextData = []
          this.textData.forEach(item => {
            if (item.text.search(this.textSearchData) !== -1)
              this.searchTextData.push(item)
            else if (item.platform.search(this.textSearchData) !== -1)
              this.searchTextData.push(item)
            else if (item.description.search(this.textSearchData) !== -1)
              this.searchTextData.push(item)
            else if (item.language.search(this.textSearchData) !== -1)
              this.searchTextData.push(item)
          })
          this.textPage.current = 1
        }
      },
      handleTextSelectionChange(val){
        this.textSelectData = val.map(item => {return item.text})
      },
      handleTextCurrentChange(val){
        this.textPage.current = val
      },
      initSelection(){
        this.textSelectData.forEach(rw => {
          this.filterTextData.forEach(item => {
            if(item.text == rw.text){
              this.$refs.acoTexts.toggleRowSelection(item ,true)
            } else {
              if(!this.docs.includes(item.text)){
                this.$refs.acoTexts.toggleRowSelection(item , false)
              }
            }
          })
        })
      },
      getTextData(){
        getTextList(this.offerId).then(res => {
          this.searchTextData = this.textData = res.data.data;
          this.textData.forEach(item => {
            this.creativeTexts.push(item.text)
          })
        }).finally(
          () => {
            let newSelectionArr = []
            this.docs.forEach(item => {
              let row = this.filterTextData.find(itm => itm.text == item)
              if(row !== undefined && row !== null){
                newSelectionArr.push(row)
              }
            })
            this.textSelectData = newSelectionArr
            if(this.creativeTextSelection){
              this.initSelection()
            }
          }
        );
      },
    }
  }
</script>
